﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using School.Entities;
using System.Collections;

namespace School.Service.Uniform
{
    public static class SRV_CLOTHES_TRANSACTIONClass
    {
        private static SchoolEntities db = new SchoolEntities();
       
        public static IEnumerable<SRV_CLOTHES_TRANSACTION> AllSRV_CLOTHES_TRANSACTION()
        {
            var query = from trans in db.SRV_CLOTHES_TRANSACTION select trans;
            return query.ToList();
        }
       
        public static bool SRV_CLOTHES_TRANSACTIONInsert(List<SRV_CLOTHES_TRANSACTION> trans)
        {
            foreach (SRV_CLOTHES_TRANSACTION item in trans)
            {
                db.AddToSRV_CLOTHES_TRANSACTION(item);
            }
           
            try
            {
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static bool SRV_CLOTHES_TRANSACTIONInsert(SRV_CLOTHES_TRANSACTION trans,out long trans_Id)
        {
           
            db.AddToSRV_CLOTHES_TRANSACTION(trans);
            try
            {
                db.SaveChanges();
               trans_Id = trans.Trans_Id;
                return true;
            }
            catch
            {
                trans_Id = 0;
                return false;
            }
        }

        public static bool SRV_TRANSACTIONInsertByCode(Hashtable table)
        {
          
            foreach (DictionaryEntry item in table)
            {
                if(db.SRV_Clothe_Sizes.Where(s=>s.Size_Code == (string)item.Key).FirstOrDefault() == null)
                    return false;
                db.AddToSRV_CLOTHES_TRANSACTION(new SRV_CLOTHES_TRANSACTION()
                {
                    Size_Id = db.SRV_Clothe_Sizes.Where(s => s.Size_Code == (string)item.Key).First().Size_Id,
                    Qty = int.Parse(item.Value.ToString()),
                    Transaction_Type = 1,
                    Date_Time = DateTime.Now
                });
                db.SRV_Clothe_Sizes.Where(s => s.Size_Code == (string)item.Key).First().QTY += int.Parse(item.Value.ToString());
            }
          
            //if (lstSizes.Count < 1)
            //    return false;
            try
            {
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
